<?php

namespace api\modules\app\models;

use Yii;

/**
 * This is the model class for table "meta_area".
 *
 * @property integer $id
 * @property string $name
 * @property string $polygon
 * @property string $polygon_gps
 * @property string $address
 * @property string $category
 * @property string $category_code
 * @property string $parent_id
 * @property string $parent_code
 * @property string $lng
 * @property string $lat
 * @property string $area_info
 * @property integer $status
 * @property integer $level
 * @property integer $route_id
 * @property integer $route_vice_id
 * @property integer $user_id
 * @property string $code
 */
class MetaArea extends \api\modules\app\models\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'meta_area';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('test_3');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id'], 'required'],
            [['id', 'status', 'level', 'route_id', 'route_vice_id', 'user_id'], 'integer'],
            [['polygon', 'polygon_gps', 'area_info'], 'string'],
            [['name', 'address', 'category', 'parent_id', 'lng', 'lat', 'category_code', 'parent_code'], 'string', 'max' => 255],
            [['code'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'polygon' => 'Polygon',
            'polygon_gps' => 'Polygon Gps',
            'address' => 'Address',
            'category' => 'Category',
            'parent_id' => 'Parent ID',
            'lng' => 'Lng',
            'lat' => 'Lat',
            'area_info' => 'Area Info',
            'status' => 'Status',
            'level' => 'Level',
            'route_id' => 'Route ID',
            'route_vice_id' => 'Route Vice ID',
            'user_id' => 'User ID',
            'code' => 'Code',
            'category' => 'Category Code'
        ];
    }

    public static function findNameById($id) {
        $model = MetaArea::findOne($id);
        return !empty($model) ? $model->name : '';
    }

    public static function findNameByCode($code) {
        $model = MetaArea::find()->where(['code' => $code])->one();
        return !empty($model) ? $model->name : '';
    }

    public static function findCodeByName($name) {
        $model = MetaArea::find()->where(['name' => $name])->one();
        return !empty($model) ? $model->code : '';
    }
}